<?php


namespace App\Internals;

use FoxLib\Exceptions\InternalServiceException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class CourseInternal
{
    public static function getCourses()
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLessonAllSources($lessonId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/lessons/{$lessonId}/sources";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLessonSection($lessonId, $section)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/lessons/{$lessonId}/sections/{$section}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLevelFirstLesson($level)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/lessons/first";
            $response = $client->request('GET', $rpcUrl, ['query' => ['level' => $level]]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLevelCourse($level)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/level/{$level}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLesson($lessonId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/lessons/{$lessonId}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLessons($lessonIds)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/lessons";
            $response = $client->request('POST', $rpcUrl, [
                'json' => [
                    'lessonIds' => $lessonIds
                ]
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function incrStudiedCount($lessonId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/lessons/{$lessonId}/studied-count";
            $response = $client->request('POST', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getTags($tagIds)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/tags";
            $response = $client->request('POST', $rpcUrl, [
                'json' => [
                    'tagIds' => $tagIds
                ]
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }


    public static function getKnowledgeById($knowledgeId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/knowledge/{$knowledgeId}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getKnowledge($knowledgeIds)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/knowledge";
            $response = $client->request('POST', $rpcUrl, [
                'json' => [
                    'knowledgeIds' => $knowledgeIds
                ]
            ]);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getLevels()
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/levels";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    // 获取某个课程所有lesson
    public static function getCourseLessons($courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/{$courseId}/lessons";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }

    public static function getCourse($courseId)
    {
        try {
            $client = new Client();
            $rpcUrl = config('app.internal_services_host.course') . "/internal/courses/{$courseId}";
            $response = $client->request('GET', $rpcUrl);
            $result = json_decode($response->getBody(), true);
            return $result['data'];
        } catch (ClientException $e) {
            throw new InternalServiceException();
        }
    }
}
